package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.OutLine;
import com.zyd.shiro.business.service.PakOutLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakOutLine;
import com.zyd.shiro.persistence.mapper.PakOutLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakOutLineServiceImpl implements PakOutLineService {

    @Autowired
    private PakOutLineMapper pakOutLineMapper;

    @Override
    public OutLine insert(OutLine outLine) {
        Assert.notNull(outLine, "对象不可为空！");
        outLine.getPakOutLine().setUpdateTime(new Date());
        outLine.getPakOutLine().setCreateTime(new Date());
        pakOutLineMapper.insertSelective(outLine.getPakOutLine());
        return outLine;
    }

    @Override
    public void insertList(List<OutLine> outLines) {
            for(OutLine outLine:outLines){
               outLine.getPakOutLine().setUpdateTime(new Date());
               outLine.getPakOutLine().setCreateTime(new Date());
               pakOutLineMapper.insertSelective(outLine.getPakOutLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakOutLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(OutLine outLine) {
        Assert.notNull(outLine, "对象不可为空！");
        outLine.setUpdateTime(new Date());
        return pakOutLineMapper.updateByPrimaryKey(outLine.getPakOutLine()) > 0;
    }

    @Override
    public boolean updateSelective(OutLine outLine) {
        Assert.notNull(outLine, "对象不可为空！");
        outLine.setUpdateTime(new Date());
        return pakOutLineMapper.updateByPrimaryKeySelective(outLine.getPakOutLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public OutLine getByPrimaryKey(Long primaryKey) {
        PakOutLine pakOutLine = pakOutLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakOutLine ? null : new OutLine(pakOutLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public OutLine getOneByEntity(OutLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakOutLine pakOutLine = pakOutLineMapper.selectOne(test.getPakOutLine());
        return null == pakOutLine ? null : new OutLine(pakOutLine);
    }

    @Override
    public List<OutLine> listAll() {
        List<PakOutLine> pakOutLines = pakOutLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakOutLines)) {
            return null;
        }
        List<OutLine> outLines = new ArrayList<>();
        for (PakOutLine pakOutLine : pakOutLines) {
            outLines.add(new OutLine(pakOutLine));
        }
        return outLines;
    }

    @Override
    public List<OutLine> listByEntity(OutLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<OutLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakOutLine> pakOutLines = pakOutLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakOutLines)) {
            return null;
        }
        List<OutLine> outLines = new ArrayList<>();
        for (PakOutLine pakOutLine : pakOutLines) {
            outLines.add(new OutLine(pakOutLine));
        }
        PageInfo bean = new PageInfo<PakOutLine>(pakOutLines);
        bean.setList(outLines);
        return bean;
    }
@Override
    public void increase(PakOutLine pakOutLine) {



        pakOutLineMapper.insert(pakOutLine);
    }




}
